package com.lvy.hczn.transmit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.lvy.hczn.front.common.constant.Constants;
import com.lvy.hczn.front.common.constant.MqConstants;
import com.lvy.hczn.front.common.exception.UtilException;
import com.lvy.hczn.front.system.domain.business.Pack;
import com.lvy.hczn.front.system.service.PackService;
import com.lvy.hczn.mq.rocket.ParamInfo;
import com.lvy.hczn.mq.rocket.ProducerUtil;
import com.lvy.hczn.transmit.domain.dto.cteck.TPack;
import com.lvy.hczn.transmit.service.TPackService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class TPackServiceImpl implements TPackService {

    @Autowired
    private PackService packService;

    @Autowired
    private ProducerUtil producerUtil;

    /**
     * 平台推送包装袋接口
     *
     * @param request
     * @param info
     */
    @Override
    @Transactional
    public void syncPackFromPlatForm(HttpServletRequest request, ParamInfo info) {
        log.error("syncPackFromPlatForm:{}", JSON.toJSONString(info));
        if (info != null && info.getList() != null && !info.getList().isEmpty()) {
            List<Pack> list = new ArrayList<>();
            List<TPack> tPackList = JSON.parseArray(JSON.toJSONString(info.getList()), TPack.class);
            tPackList.forEach(item -> {
                Pack pack = new Pack();
                BeanUtil.copyProperties(item, pack);
                if (item.getType() == 2) {
                    pack.setDelFlag(Constants.DEL_FLAG_INVALID);
                } else {
                    if (StrUtil.isEmpty(item.getName())) {
                        throw new UtilException("包装袋名称为空");
                    }
                    if (item.getPrice() == null || item.getPrice().compareTo(BigDecimal.ZERO) <= 0) {
                        throw new UtilException("包装袋价格不能为空且大于0");
                    }
                    if (item.getVolume() == null || item.getVolume().compareTo(BigDecimal.ZERO) <= 0) {
                        throw new UtilException("包装袋容积不能为空且大于0");
                    }
                }

                pack.setSourceId(item.getId() + Constants.SOURCE_ID_DEFAULT);
                Pack packModel = packService.selectPackById(pack.getId());
                if (packModel != null) {
                    packService.updateById(pack);
                } else {
                    packService.insertPack(pack);
                }

                list.add(pack);
            });
            ParamInfo<Pack> paramInfo = new ParamInfo<>();
            paramInfo.setList(list);
            try {
                producerUtil.sendAsyncMsg(MqConstants.TOPIC_PACK, null, JSON.toJSONString(paramInfo).getBytes(StandardCharsets.UTF_8), IdUtil.fastSimpleUUID());
            } catch (Exception e) {
                throw new UtilException("信息转发异常");
            }
        } else {
            log.error("syncPackFromPlatFormError:{}", JSON.toJSONString(info));
            throw new UtilException("数据信息为空");
        }
    }

    /**
     * 包装袋同步到仓库
     *
     * @param request
     * @param info
     * @return void
     * @author yw
     * @date 2023-07-18 15:22:18
     */
    @Override
    public List<Pack> syncPackFromTransmit(HttpServletRequest request, ParamInfo<Pack> info) {
        return packService.list();
    }
}
